package com.app.oral.config

import com.app.oral.model.FileDeleteStatus
import com.app.oral.model.FileDeleteSourceType
import org.apache.ibatis.type.BaseTypeHandler
import org.apache.ibatis.type.JdbcType
import java.sql.CallableStatement
import java.sql.PreparedStatement
import java.sql.ResultSet

/**
 * 文件删除状态类型处理器
 */
class FileDeleteStatusTypeHandler : BaseTypeHandler<FileDeleteStatus>() {
    
    override fun setNonNullParameter(ps: PreparedStatement, i: Int, parameter: FileDeleteStatus, jdbcType: JdbcType?) {
        ps.setString(i, parameter.name)
    }
    
    override fun getNullableResult(rs: ResultSet, columnName: String): FileDeleteStatus? {
        val status = rs.getString(columnName)
        return status?.let { FileDeleteStatus.valueOf(it) }
    }
    
    override fun getNullableResult(rs: ResultSet, columnIndex: Int): FileDeleteStatus? {
        val status = rs.getString(columnIndex)
        return status?.let { FileDeleteStatus.valueOf(it) }
    }
    
    override fun getNullableResult(cs: CallableStatement, columnIndex: Int): FileDeleteStatus? {
        val status = cs.getString(columnIndex)
        return status?.let { FileDeleteStatus.valueOf(it) }
    }
}

/**
 * 文件删除来源类型处理器
 */
class FileDeleteSourceTypeHandler : BaseTypeHandler<FileDeleteSourceType>() {
    
    override fun setNonNullParameter(ps: PreparedStatement, i: Int, parameter: FileDeleteSourceType, jdbcType: JdbcType?) {
        ps.setString(i, parameter.name)
    }
    
    override fun getNullableResult(rs: ResultSet, columnName: String): FileDeleteSourceType? {
        val sourceType = rs.getString(columnName)
        return sourceType?.let { FileDeleteSourceType.valueOf(it) }
    }
    
    override fun getNullableResult(rs: ResultSet, columnIndex: Int): FileDeleteSourceType? {
        val sourceType = rs.getString(columnIndex)
        return sourceType?.let { FileDeleteSourceType.valueOf(it) }
    }
    
    override fun getNullableResult(cs: CallableStatement, columnIndex: Int): FileDeleteSourceType? {
        val sourceType = cs.getString(columnIndex)
        return sourceType?.let { FileDeleteSourceType.valueOf(it) }
    }
}